#!/usr/bin/env python
# encoding: utf-8
# @author: Lin Han
# @contact: voldemort22@126.com
# @file: crud.py
# @time: 2021/4/13 15:42
# @desc:
import re
from datetime import datetime
from typing import Dict

from application.history_clamp.crud import retrieve_all_scene_redis_values
from utils.mongo_tools import retrieve_all_mongo_values
from utils.oss.oss_tools import download_file_from_oss
from utils.utils import read_json
from views.configs import JSON_OSS_ADDRESS, SCENE_COLLECTION


def generate_standard_clamp_data(
        number: str,
        file_path: str,
        desc: str = "",
        version: str = "1.0",
        isFeature: bool = False,
        isFlag: bool = False,
) -> Dict:
    """

    :param file_path: 夹具在OSS上的地址
    :param number:
    :param isFlag:
    :param isFeature:
    :param desc:
    :param version:
    """
    model = {
        "number": number,
        "time": datetime.now().strftime("%Y-%m-%d %H:%M"),
        # 存到数据库的name字段只应当包含最后一层的名字
        "name": file_path.split("/")[-1],
        "desc": desc,
        "type": "3",
        "version": version,
        "isFeature": isFeature,
        "isFlag": isFlag,
        "file": {
            JSON_OSS_ADDRESS: file_path,
            "transferFormatMessage": "not transfered yet",
        },
    }

    return model


def generate_standard_clamp_data_v3(
        number: str,
        file_path: str,
        desc: str = "",
        version: str = "2.0",
        isFeature: bool = False,
        isFlag: bool = False,
) -> Dict:
    """

    :param file_path: 夹具在OSS上的地址
    :param number:
    :param isFlag:
    :param isFeature:
    :param desc:
    :param version:
    """
    download_file_from_oss(file_path)
    data = read_json(file_path)
    model = {
        "number": number,
        "time": datetime.now().strftime("%Y-%m-%d %H:%M"),
        # 存到数据库的name字段只应当包含最后一层的名字
        "name": file_path.split("/")[-1],
        "desc": desc,
        "type": "3",
        "version": version,
        "isFeature": isFeature,
        "isFlag": isFlag,
        "file": {
            JSON_OSS_ADDRESS: file_path,
            "transferFormatMessage": "not transfered yet",
        },
        "tag": {},
        "data": data,
    }

    return model


def filter_standard_clamp_under_20(result: list):
    filtered_result = []
    for i in result:
        if int(re.search("[0-9]{2}", result).group(0)) < 21:
            filtered_result.append(i)
    return filtered_result
